﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.Sql;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DbConnection
/// </summary>
public static class DbConnection
{
    //private const string connectionString = "data source=ASHRAF\\SQLEXPRESS;initial catalog=imadethis;User ID=nour;Password=nour;";
    private const string connectionString = "data source=.\\SQLEXPRESS;initial catalog=imadethis;User ID=nour;Password=nour;";
    private static SqlConnection _conn;
    private static SqlDataReader _reader;
        
    public static void Connect()
    {
        _conn = new SqlConnection(connectionString);
        _conn.Open();
    }

    public static SqlDataReader ExecuteSelect(string sql)
    {
        if (_reader != null && !_reader.IsClosed)
            _reader.Close();

        SqlCommand command = new SqlCommand(sql,_conn);
        _reader = command.ExecuteReader();

        return _reader;
    }

    public static void ExecuteNonQuery(string sql)
    {
        if (_reader != null && !_reader.IsClosed)
            _reader.Close();

        SqlCommand command = new SqlCommand(sql, _conn);
        command.ExecuteNonQuery();
    }

    public static void Close()
    {
        _conn.Close();
    }
}
